package com.atguigu.eduservice.service.impl;

import com.atguigu.common.R;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.feign.VodFeign;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2021-01-06
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodFeign vodFeign;

    /**
     * 根据课程id删除小节以及视频
     *
     * @param courseId
     */
    @Override
    public void removeVideoByCourseId(String courseId) {
        deleteVideoByCourseId(courseId);
    }

    private void deleteVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> qw = new QueryWrapper<EduVideo>();
        qw.eq("course_id", courseId);
        List<EduVideo> eduVideos = baseMapper.selectList(qw);
        List<String> sourceIds = new ArrayList<>();
        if (eduVideos.size()>0){
            eduVideos.forEach((video -> {
                if (!StringUtils.isBlank(video.getVideoSourceId())) {
                    sourceIds.add(video.getVideoSourceId());
                }
            }));
            vodFeign.removeBatch(sourceIds);
        }
        baseMapper.delete(qw);
    }

}
